<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<head>
    <meta charset="UTF-8">
    <title>应用系统B</title>
</head>
<body>
<div id="currentHost"></div>
<div id="token"></div>
<div style="align-content: center">当前登录用户：<span id="loginStatus"></span></div>
<div>
    <button onclick="test()">测试接口访问</button>
    <button onclick="clearToken()">注销登录</button>
</div>

<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/Base64/1.0.2/base64.min.js"></script>
<script>
    //本页面用于回调中转
    var token = getUrlParam("token");
    if (token==null){
        token = localStorage.getItem("token");
    }
    else{
       localStorage.setItem("token",token);
    }
    var info = token.split(".")[1];
    var userName = JSON.parse(window.atob(info)).name;
    $('#loginStatus').text(userName);
    $('#token').text(token);
    $('#currentHost').text("当前ip，端口：" + location.origin);

    //测试token是否有效
    function test() {
        $.get("/test?token=" +  localStorage.getItem("token"), function (res) {
            if (res.code === 1) {
                alert("登录成功");
            } else {
                alert("未登录");
            }
        })
    }

    //注销登录
    function clearToken() {
        window.location.href = "/logout?token=" +  localStorage.getItem("token");
    }

    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null)
            return unescape(r[2]);
        return "index"; //返回参数值
    }
</script>
</body>
</html>